import Poster from '../../../miniprogram_dist/poster/poster';
const utils = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jdConfig: {
      width: 679,
      height: 900,
      debug: false,
      backgroundColor: "#fff",
      images: [{}],
      texts: [{
        x: 225,
        y: 43,
        text: "",
        fontSize: 28,
        textAlign: "left",
        baseLine: "middle",
        lineNum:1,
        width:350,
        zIndex: 2
      },
        {
          x: 225,
          y: 110,
          text: "",
          fontSize: 36,
          textAlign: "left",
          baseLine: "middle",
          lineNum: 1,
          width: 359,
          zIndex: 2
        },
        {
          x: 225,
          y: 175,
          text: "",
          fontSize: 36,
          textAlign: "left",
          baseLine: "middle",
          lineNum: 1,
          width: 359,
          zIndex: 2
        },
        {
          x: 340,
          y: 830,
          text: "企动 ● 玖玖客",
          fontSize: 28,
          textAlign: "center",
          baseLine: "middle",
          color: '#888',
          lineNum: 1,
          zIndex: 2
        }
      ],
    },
    myInfo: {},
    staffid: 0,
    tempFilePath:""
  },
  /**
   * 异步生成海报
   */
  onCreatePoster(obj) {
    var jdConfig = this.data.jdConfig;
    var images = [{
      url: obj.ShowAvater,
      width: 162,
      height: 162,
      borderRadius: 10,
      x: 30,
      y: 30,
      zIndex: 1
    },
      {
        url: obj.QrCode,
        width: 460,
        height: 460,
        x: 110,
        y: 280,
        zIndex: 1
      }
    ]
    jdConfig.texts[0].text = obj.Company
    jdConfig.texts[1].text = obj.Name
    jdConfig.texts[2].text = obj.Position
    jdConfig.images = images
    this.setData({
      jdConfig: jdConfig
    }, () => {
      Poster.create();
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.staffid) {
      this.setData({
        staffid: options.staffid
      })
    }
    this.getMyCardInfo(options.staffid);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  //获取个人信息
  getMyCardInfo: function(staffid) {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/staff/getinfo?id=' + staffid,
      success: function(res) {
        if (res.data.Code == 0) {
          var obj = res.data.Data;
          let slider = '';
          if(obj.IndustryCode > 0 && obj.Slides){
            slider = obj.Slides.split(',');
            obj.ShowAvater = utils.formatAvater(slider[0], "https://static.711688.net.cn/upload/card/source/");
          }else{
            obj.ShowAvater = utils.formatAvater(obj.Avater, "https://static.711688.net.cn/upload/card/source/");
          }
          that.setData({
            myInfo: obj
          })
          that.onCreatePoster(obj)
        }
      },
    })
  },

  onPosterSuccess(e) {
    this.setData({
      tempFilePath: e.detail
    })
  },

  onPosterFail(err) {
  },

  onPosterSaveImage() {
    var that = this;
    wx.saveImageToPhotosAlbum({
      filePath: that.data.tempFilePath,
      success(res) {
        wx.showModal({
          content: '图片已保存到相册，赶紧晒一下吧~',
          showCancel: false,
          confirmText: '好的',
          confirmColor: '#333',
          success: function (res) {
            if (res.confirm) { }
          },
          fail: function (res) { }
        })
      },
      fail: function (res) {
        wx.showToast({
          title: '请删掉小程序再重新授权允许保存至相册',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

})